import 'dart:convert';

class A {
  String str = 'str';

  A(this.str);

  A.fromJSON(String string) {
    var s = json.decode(string);
    this.str = s['str'];
  }

  Map toJson() {
    return {'str': str};
  }

  @override
  String toString() {
    return "-";//json.encode(toJson());
  }
}

void jsonEncodeTest() {
  print(json.encode(List.filled(10,10)));
  print(json.encode(List.filled(10,A("wocao"))));
  print(json.encode(1));
  print(json.encode('1'));
  print(json.encode(true));
  print(json.encode({}));
  print(json.encode([1, 2, 3]));
  print(json.encode([
    1,
    '2',
    true,
    [1, 2, 3]
  ]));
  //Converting object to an encodable object failed: Instance of '_CompactLinkedHashSet<String>'
  //print(json.encode({'fluorine', 'chlorine', 'bromine', 'iodine', 'astatine'}));

  print(json.encode({
    'first': 'partridge',
    'second': 'turtledoves',
    'fifth': 'golden rings'
  }));
  //Converting object to an encodable object failed: _LinkedHashMap len:3
  // print(json.encode({1: 1, 2: '2', 3: true}));
  print(json.encode({
    'first': 1,
    'second': 'turtledoves',
    'fifth': false,
    '5': {'1': 1, '2': 2},
    '6': [1, 2, true]
  }));

  // print(json.encode(A('hello world')));
  // print(A.fromJSON(json.encode(A('hello JSON'))));
}

void jsonDecodeTest() {
  var str =
      '{"first":1,"second":"turtledoves","fifth":false,"5":{"1":1,"2":2},"6":[1,2,true]}';
  var decode = json.decode(str);
  print(decode);
  print('${decode['first']} ${decode['second']}');
}

void main() {
  jsonEncodeTest();
  jsonDecodeTest();
}
